local extension = Package("PVE2")
extension.extensionName = "tshu"

Fk:loadTranslationTable{
  ["PVE2"] = "天书.庆典",
}

local U = require "packages/utility/utility"
local T = require "packages/tshu/record/Theorem"
local T2 = require "packages/jlve/record/Theorem"

local nianzhuge = General(extension, "tshujl__nianzhugeliang","shu",0,0)
nianzhuge.hidden = true
local xinghan = fk.CreateTriggerSkill{
  name = "tshu__xinghan",
  anim_type = "control",
  global = true,
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.TurnStart,fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("tshujl__nianzhugeliang") > 0 and player:usedSkillTimes(self.name, Player.HistoryTurn) < 2 and math.random(1,10) <= 8
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name,nil,"定乱：你可以增加一点体力上限并回复一点体力，然后随机变更一名角色势力并摸2~4张牌。")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player,1)
    if player:isWounded() and not player.dead then
        room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
        }
    end
    if not player.dead then
      player:drawCards(math.random(2,4),self.name)
    end
    local ps = {}
    for _, p in ipairs(room.alive_players) do
      if p.kingdom ~= "shu" then
        table.insert(ps,p)
      end
    end
    if #ps > 0 then
      room:changeKingdom(table.random(ps), "shu", true)
    end
  end,
}
local liaoyuan = fk.CreateTriggerSkill{
  name = "tshu__liaoyuan",
  anim_type = "offensive",
  global = true,
  mute = true,
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart,fk.CardUsing,fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if player.dead then return end
    if event == fk.EventPhaseStart then
      return target == player and player:getMark("tshujl__nianzhugeliang") > 0 and  not player:prohibitUse(Fk:cloneCard("fire_attack")) and player.phase == Player.Play and math.random(1,20) <= 17
    elseif event == fk.CardUsing then
      return target == player and player:getMark("tshujl__nianzhugeliang") > 0 and data.card.name == "fire_attack" and math.random(1,20) <= 17 
    elseif event == fk.DamageCaused then
      return target == player and player:getMark("tshujl__nianzhugeliang") > 0 and data.card and data.card.name == "fire_attack" and not data.chain and math.random(1,20) <= 17  and player:getMark("tshu__liaoyuan-phase") < 6 
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      return player.room:askForSkillInvoke(player, self.name,nil,"燎原：你可以视为使用一张【火攻】。")
    elseif event == fk.CardUsing then
      local targets = player.room:getUseExtraTargets( data)
      local n = #targets
      if n == 0 then return false end
      local tos = room:askForChoosePlayers(player, targets, 1, n, "#tshu__liaoyuan-choose:::"..data.card:toLogString(), self.name, true)
      if #tos > 0 then
        self.cost_data = tos
        return true
      end
    elseif event == fk.DamageCaused then
      local n = 0
      for _, p in ipairs(room.alive_players) do
        if p.kingdom == "shu" then
          n = n + 1
        end
      end
      n = math.max(1,n)
      self.cost_data = n
      return player.room:askForSkillInvoke(player, self.name,nil,"燎原：是否令对"..Fk:translate(data.to.general).."的伤害+"..n.."，然后本阶段出【杀】次数+1且随机获得1~3张锦囊牌？")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:broadcastSkillInvoke(self.name)
      room:notifySkillInvoked(player,self.name,"offensive")
      U.askForUseVirtualCard(room, player, "fire_attack", nil, self.name)
    elseif event == fk.CardUsing then
      table.forEach(self.cost_data, function (id)
        table.insert(data.tos, {id})
      end)
    elseif event == fk.DamageCaused then
      player:broadcastSkillInvoke(self.name)
      data.damage = data.damage + self.cost_data
      room:notifySkillInvoked(player,self.name,"drawCards")
      room:addPlayerMark(player,"tshu__liaoyuan-phase",1)
      local cards = room:getCardsFromPileByRule(".|.|.|.|.|trick",math.random(1,3),"allPiles")
      if #cards > 0 then
      room:moveCards({
        ids = cards,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = self.name,
      })
      end
    end
  end,
}
local liaoyuan_targetmod = fk.CreateTargetModSkill{
  name = "#tshujl__liaoyuan_targetmod",
  global = true,
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and player:getMark("tshu__liaoyuan-phase") > 0 then
      return player:getMark("tshu__liaoyuan-phase")
    end
    return 0
  end,
}
liaoyuan:addRelatedSkill(liaoyuan_targetmod)
nianzhuge:addSkill(xinghan)
nianzhuge:addSkill(liaoyuan)
Fk:loadTranslationTable{
  ["tshujl__nianzhugeliang"] = "念·诸葛亮",
  ["tshujl__nianzhuge_choice"] = "诸葛亮",

  ["tshu__xinghan"] = "定乱",
  [":tshu__xinghan"] = "锁定技，回合开始时或当你受到伤害后，你有80%概率增加一点体力上限并回复一点体力，然后摸2~4张牌并随机将场上一名非“蜀”势力角色变更为“蜀”势力（每回合限触发两次）。",

  
  ["tshu__liaoyuan"] = "燎原",
  [":tshu__liaoyuan"] = "锁定技，出牌阶段开始时你有85%的概率可以视为使用一张【火攻】，且你使用【火攻】时85%概率可以额外指定任意名角色成为目标；<br>每当你使用【火攻】造成伤害时，有85%概率令此伤害增加等同场上“蜀”势力角色数(至少为1），然后你本阶段出【杀】次数+1并从牌堆或弃牌堆随机获得1~3张锦囊牌（此项每阶段限触发6次）。",

  ["#tshu__liaoyuan-choose"] = "燎原：你可以多选择任意名角色成为 %arg 的目标。",
  [":tshujl__nianzhugeliang"] = "<br>〖定乱〗:锁定技，回合开始时或当你受到伤害后，你有80%概率增加一点体力上限并回复一点体力，然后摸2~4张牌并随机将场上一名非“蜀”势力角色变更为“蜀”势力（每回合限触发两次）。"..
  "<br>〖燎原〗:锁定技，出牌阶段开始时你有85%的概率可以视为使用一张【火攻】，且你使用【火攻】时85%概率可以额外指定任意名角色成为目标；<br>每当你使用【火攻】造成伤害时，有85%概率令此伤害增加等同场上“蜀”势力角色数(至少为1），然后你本阶段出【杀】次数+1并从牌堆或弃牌堆随机获得1~3张锦囊牌（此项每阶段限触发6次）。",

  ["$tshu__xinghan"] = "料事如神，随机应变。",
  ["$tshu__liaoyuan"] = "黄龙翔天，离火灭世！",
}


local miaolvlingqi = General(extension, "tshujl__miaolvlingqi","qun",0,0)
miaolvlingqi.hidden = true
local guowu = fk.CreateTriggerSkill{
  name = "tshujl__guowu",
  anim_type = "offensive",
  global = true,
  mute = true,
  priority = 11,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("tshujl__miaolvlingqi") ~= 0 and not player.dead and player.phase == Player.Play 
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player,self.name,nil,"帼武：是否获得技能〖无双〗并令你本阶段使用牌无距离次数限制，且可多指定两名目标或令使用的牌额外结算一次。")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player,self.name)
    player:broadcastSkillInvoke(self.name,math.random(1,2))
    if not player:hasSkill("wushuang",true) then
      room:addPlayerMark(player,"tshujl__miaolvlingqi-turn", 1)
      room:handleAddLoseSkills(player,"wushuang")
    end
    room:addPlayerMark(player,"tshujl__miaolvlingqi-phase", 1)
  end,
  refresh_events = {fk.TurnEnd},
  can_refresh = function (self, event, target, player, data)
    return player:getMark("tshujl__miaolvlingqi-turn") > 0 
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:handleAddLoseSkills(player,"-wushuang")
  end,
}
local guowu_delay = fk.CreateTriggerSkill{
  name = "#tshujl__guowu_delay",
  anim_type = "offensive",
  global = true,
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.CardUseFinished,fk.AfterCardTargetDeclared},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("tshujl__miaolvlingqi") == 0 or player:getMark("tshujl__miaolvlingqi-phase") == 0 or player.dead then return end
    if event == fk.CardUseFinished then
      if target == player and player:getMark("tshujl__miaolvlingqi-phase") > 0 and not player.dead and
        (data.card:isCommonTrick() or table.contains({"slash","peach","analeptic"},data.card.trueName)) then
          return not table.contains(data.card.skillNames,self.name)
      end
    elseif event == fk.AfterCardTargetDeclared then
      return target == player and player:getMark("tshujl__miaolvlingqi-phase") > 0 and not player.dead and
      (data.card:isCommonTrick() or data.card.trueName == "slash") and #player.room:getUseExtraTargets( data) > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.CardUseFinished then
      return player.room:askForSkillInvoke(player,self.name,nil,"帼武：是否令【"..Fk:translate(data.card.name).."】额外执行一次效果？")
    elseif event == fk.AfterCardTargetDeclared then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      room:notifySkillInvoked(player,self.name)
      player:broadcastSkillInvoke(guowu.name,math.random(5,6))
      local name = Fk:cloneCard(data.card.name)
      name.skillName = self.name
      local use ={
      from = player.id,
      tos = data.tos,
      card = name,
      extraUse = true
      }
      room:useCard(use)
    elseif event == fk.AfterCardTargetDeclared then
      local targets = player.room:getUseExtraTargets( data)
      if #targets == 0 then return false end
      local tos = room:askForChoosePlayers(player, targets, 1, 2, "#tshujl__guowu-choose:::"..data.card:toLogString(), guowu.name, true)
      player:broadcastSkillInvoke(guowu.name,math.random(3,4))
      if #tos > 0 then
      table.forEach(tos, function (id)
        table.insert(data.tos, {id})
      end)
      end
    end
  end,
}
local guowu_targetmod = fk.CreateTargetModSkill{
  name = "#tshujl__guowu_targetmod",
  global = true,
  priority = 11,
  bypass_times =  function(self, player)
    return player:getMark("tshujl__miaolvlingqi-phase") > 0
  end,
  bypass_distances =  function(self, player)
    return player:getMark("tshujl__miaolvlingqi-phase") > 0
  end,
}

local zhuangrong = fk.CreateTriggerSkill{
  name = "tshujl__zhuangrong",
  anim_type = "drawcard",
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.HpChanged,fk.AfterCardsMove},
  global = true,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("tshujl__miaolvlingqi") == 0 or player:usedSkillTimes(self.name, Player.HistoryTurn) ~= 0 or player.dead or ((event == fk.HpChanged and player.hp > 1 )or(event == fk.AfterCardsMove and player:getHandcardNum() > 1)) then return end
    if event == fk.HpChanged then
      return target == player and  player.hp <= 1
    elseif event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return player:getHandcardNum() <= 1
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player,self.name,nil,"妆戎：是否将体力值和手牌数补至体力上限？(手牌至多补至100张)")
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = math.min(100,player.maxHp) - player:getHandcardNum()
    if n > 0 then
      player:drawCards(n,self.name)
    end
    local m = player.maxHp - player.hp
    if m > 0 and not player.dead then
      player.room:recover{
        who = player,
        num = m,
        recoverBy = player,
        skillName = self.name
      }
    end
  end,
}
guowu:addRelatedSkill(guowu_delay)
guowu:addRelatedSkill(guowu_targetmod)
miaolvlingqi:addSkill(guowu)
miaolvlingqi:addSkill(zhuangrong)
Fk:loadTranslationTable{
  ["tshujl__miaolvlingqi"] = "喵·吕玲绮",
  ["tshujl__miaolvlingqi_choice"] = "吕玲绮",
  ["#tshujl__guowu-choose"] = "帼武：你可以为%arg增加至多两个目标",
  ["tshujl__guowu"] = "帼武",
  ["#tshujl__guowu_delay"] = "帼武",
  ["#tshujl__guowu_targetmod"] = "帼武",
  [":tshujl__guowu"] = "锁定技，你的出牌阶段开始时，你本回合可以获得技能〖无双〗，然后令你此阶段使用牌时：无距离次数限制，使用【桃】、【酒】、【杀】或普通锦囊牌时可额外指定两个目标且可令此牌额外结算一次。",
  ["tshujl__zhuangrong"] = "妆戎",
  [":tshujl__zhuangrong"] = "锁定技，当你的体力值或手牌张数降至1点及以下时，你可以将体力值和手牌数补至体力上限(每回合限触发一次）。",
  [":tshujl__miaolvlingqi"] = "<br>〖帼武〗:锁定技，你的出牌阶段开始时，你本回合可以获得技能〖无双〗，然后令你于此阶段使用牌时：无距离次数限制，使用【桃】、【酒】、【杀】或普通锦囊牌时可额外指定两个目标且可令此牌额外结算一次。"..
  "<br>〖妆戎〗:锁定技，当你的体力值或手牌张数降至1点及以下时，你可以将体力值和手牌数补至体力上限(每回合限触发一次）。",

  ["$tshujl__guowu1"] = "吕家女儿胜须眉，胭脂红妆配武裳！",
  ["$tshujl__guowu2"] = "古识父子今识我，红袖弄钺话铿锵！",
  ["$tshujl__guowu3"] = "策烈马以啸西风，率锐士而擒敌酋。",
  ["$tshujl__guowu4"] = "握长兵以斩白日，横画戟而断流星。",
  ["$tshujl__guowu5"] = "被兕甲，跨良骥，挥画戟，踏兖冀。",
  ["$tshujl__guowu6"] = "束长缨，率骠骑，奔千里，破万兵。",
  ["$tshujl__zhuangrong1"] = "吕家有女初长成，戟出名扬天下惊！",
  ["$tshujl__zhuangrong2"] = "试问天下武夫，谁配画戟方天！",
}

local doupo = fk.CreateTriggerSkill{
  name = "tshujl__doupo",
  anim_type = "offensive",
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.TurnStart,fk.Damaged},
  global = true,
  can_trigger = function(self, event, target, player, data)
    if target ~= player or player:getMark("tshujl__nianlvbu") == 0 or player:usedSkillTimes(self.name, Player.HistoryTurn) ~= 0 or player.dead then return end
    return not player:isKongcheng()
  end,
  on_cost = function (self, event, target, player, data)
    local tars = {}
    for _, p in ipairs(player.room:getOtherPlayers(player)) do
      if not player:isProhibited(p,Fk:cloneCard "duel") then
        table.insertIfNeed(tars,p.id)
      end
    end
    if #tars > 0 then
      local tar = player.room:askForChoosePlayers(player,tars,1,#tars,"斗破：你可以选择任意名其他角色成为目标，然后你可将手牌依次当做无法响应且基础伤害增加的【决斗】对其使用。")
      if #tar > 0 then
        self.cost_data = tar
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mark = type(player:getMark("@[tshu__tianshu]")) == "table" and player:getMark("@[tshu__tianshu]") or {}
    local skills = {}
    for _, s in ipairs(player.player_skills) do
      if not (s.attached_equip or s.name[#s.name] == "&" or string.sub(s.name, 1,1) == "#") and not table.contains(mark,s.name) then
        table.insert(skills,s.name)
      end
    end
    if #skills < 1 or player.dead then return end
    local gskills = T2.choosemarkskillask(player,skills,room,0,1,"移除技能加伤")
    local n = 0
    if  gskills and #gskills >= 1 then 
      player:broadcastSkillInvoke(self.name)
      room:handleAddLoseSkills(player, "-"..table.concat(gskills, "|-"), nil)
      n = n + #gskills
    end
    room:setPlayerMark(player,self.name.."-phase",n)
    for i = 1, 999, 1 do
      if player.dead or player:isKongcheng() then break end
      local tars = {}
      for _, pid in ipairs(self.cost_data) do
        local p = room:getPlayerById(pid)
        if not p.dead and not player:isProhibited(p,Fk:cloneCard "duel")  then
          table.insertIfNeed(tars,pid)
        end
      end
      if #tars <= 0 then break end
      local card = room:askForCard(player,1,1,false,self.name,true,".","斗破：选择一张手牌，然后将其作为【决斗】对你选择的目标使用。")
      if #card <= 0 then break end
      local duel = Fk:cloneCard "duel"
      duel.skillName = self.name
      duel.disresponsive = true
      duel.additionalDamage = (duel.additionalDamage or 0) + n
      card = Fk:getCardById(card[1])
      duel:addSubcard(card)
      local tar = {}
      for _, pid in ipairs(self.cost_data) do
        local p = room:getPlayerById(pid)
        if not p.dead and not player:isProhibited(p,Fk:cloneCard "duel") and U.canUseCardTo(room, player,p,duel, false,false) then
          table.insertIfNeed(tar,p)
        end
      end
      if #tar > 0 then
        room:useVirtualCard("duel", {card}, player,tar, self.name, true)
      else
        break
      end
    end
    room:setPlayerMark(player,self.name.."-phase",0)
  end,
  refresh_events = {fk.CardUsing},
  can_refresh = function (self, event, target, player, data)
    return target and target == player and data.card.name == "duel" and table.contains(data.card.skillNames,self.name)
  end,
  on_refresh = function (self, event, target, player, data)
    local n = player:getMark(self.name.."-phase") or 0
    data.disresponsiveList = table.map(player.room.players,Util.IdMapper)
    data.additionalDamage = (data.additionalDamage or 0) + n
  end,
}
local tazheng = fk.CreateTriggerSkill{
  name = "tshujl__tazheng",
  anim_type = "offensive",
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.Damage},
  global = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("tshujl__nianlvbu") > 0 and  player:usedSkillTimes(self.name, Player.HistoryTurn) < 2 and not player.dead and data.to and data.to ~= player and not data.to.dead
  end,
  on_cost = function (self, event, target, player, data)
    local n = data.damage
    return player.room:askForSkillInvoke(player,self.name,nil,"踏阵：你可以令 "..Fk:translate(data.to.general).." 直到你的下回合开始前无法对你使用牌，并可偷取其至多 "..n.." 个技能。")
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mark = type(player:getMark(self.name)) == "table" and player:getMark(self.name) or {}
    table.insertIfNeed(mark,data.to.id)
    room:setPlayerMark(player,self.name,mark)
    local mark2 = type(data.to:getMark("@@"..self.name)) == "table" and data.to:getMark("@@"..self.name) or {}
    table.insertIfNeed(mark2,player.id)
    room:setPlayerMark(data.to,"@@"..self.name,mark2)
    local skills = {}
    for _, s in ipairs(data.to.player_skills) do
      if not (s.attached_equip or s.name[#s.name] == "&" or string.sub(s.name, 1,1) == "#") then
        table.insertIfNeed(skills,s.name)
      end
    end
    if #skills <= 0 then return end
    local gskills = T2.choosemarkskillask(player,skills,room,0,data.damage,"选择 "..Fk:translate(data.to.general).." 至多"..data.damage.."个技能偷取之")
    if  gskills and #gskills >= 1 then 
      room:handleAddLoseSkills(data.to, "-"..table.concat(gskills, "|-"), nil)
      room:handleAddLoseSkills(player,table.concat(gskills, "|"), nil)
    end
  end,
  refresh_events = {fk.TurnStart,fk.BuryVictim},
  can_refresh = function (self, event, target, player, data)
    return target == player and (type(player:getMark(self.name)) == "table" and #player:getMark(self.name) > 0)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = type(player:getMark(self.name)) == "table" and player:getMark(self.name) or {}
    if #mark > 0 then
      room:setPlayerMark(player,self.name,0)
      for _, pid in ipairs(mark) do
        local p = room:getPlayerById(pid)
        local mark2 = type(p:getMark("@@"..self.name)) == "table" and p:getMark("@@"..self.name) or {}
        table.removeOne(mark2,player.id)
        if #mark2 > 0 then
          room:setPlayerMark(p,"@@"..self.name,mark2)
        else
          room:setPlayerMark(p,"@@"..self.name,0)
        end
      end
    end
  end,
}
local tazheng_buff = fk.CreateProhibitSkill{
  name = "#tshujl_tazheng_buff",
  frequency = Skill.Compulsory,
  global = true,
  is_prohibited = function(self, from, to, card)
    local m = type(to:getMark(tazheng.name)) == "table" and to:getMark(tazheng.name) or {}
    if table.contains(m,from.id) then
      return true
    end
  end,
}
tazheng:addRelatedSkill(tazheng_buff)
local nianlvbu = General(extension, "tshujl__nianlvbu","qun",0,0)
nianlvbu.hidden = true
nianlvbu:addSkill(doupo)
nianlvbu:addSkill(tazheng)
Fk:loadTranslationTable{
  ["tshujl__nianlvbu"] = "念·吕布",
  ["tshujl__nianlvbu_choice"] = "吕布",

  ["tshujl__doupo"] = "斗破",
  [":tshujl__doupo"] = "锁定技，你的回合开始时或当你受到伤害后，你可以选择任意个目标并移除任意个技能，然后可以依次将手牌作为不可响应且基础伤害增加移除技能数的【决斗】对目标使用(每回合限触发一次)。",
  ["tshujl__tazheng"] = "踏阵",
  ["@@tshujl__tazheng"] = "踏阵",
  ["#tshujl_tazheng_buff"] = "踏阵",
  [":tshujl__tazheng"] = "锁定技，当你造成伤害后，你可以令目标直到你的下回合开始前无法对你使用牌，然后你可偷取目标至多等同此次伤害量个技能(每回合限触发两次）。",
  [":tshujl__nianlvbu"] = "<br>〖斗破〗:锁定技，你的回合开始时或当你受到伤害后，你可以选择任意个目标并移除任意个技能，然后可以依次将手牌作为不可响应且基础伤害增加移除技能数的【决斗】对目标使用(每回合限触发一次)。"..
  "<br>〖踏阵〗:锁定技，当你造成伤害后，你可以令目标直到你的下回合开始前无法对你使用牌，然后你可偷取目标至多等同此次伤害量个技能(每回合限触发两次）。",
  ["$tshujl__doupo"] = "辕门射戟，天下无双。",
  ["$tshujl__tazheng"] = "人不为己，天诛地灭！",
}

local sajia = General(extension, "tshujl__sajia","hundun",0,0)
sajia.hidden = true
local chuangshi_active = fk.CreateActiveSkill{
  name = "#tshujl_chuangshi_active",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  interaction = function()
    if Self:getMark("tshujl__chuangshi_choose") < 5 then
      return UI.ComboBox {choices = {"tshujl_chuangshi_random","tshujl_chuangshi_choose"}}
    else
      return UI.ComboBox {choices = {"tshujl_chuangshi_random"}}
    end
  end,
}
local chuangshi = fk.CreateTriggerSkill{
  name = "tshujl__chuangshi",
  anim_type = "masochism",
  global = true,
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.TurnStart,fk.Damaged},
  can_trigger = function(self, event, target, player, data)
  if player.dead then return false end
    return target == player and player:getMark("tshujl__sajia") > 0 and math.random(1,10) <= 9 and player:usedSkillTimes(self.name, Player.HistoryTurn) < 2
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if player.id > 0 and player:getMark("tshujl__chuangshi_choose") < 5 then
    local n =  player:getMark("tshujl__chuangshi_choose") or 0
    n = 5 - n
      local _, ret = room:askForUseActiveSkill(player,"#tshujl_chuangshi_active", "创世：你可以重新从七张武将牌中获得并立即触发一个技能(剩 "..n.." 次自选机会)。", true)
      if ret then
        self.cost_data = ret.interaction
        return true
      end
    else
      self.cost_data = "tshujl_chuangshi_random"
      return room:askForSkillInvoke(player,self.name,nil,"创世：你可以重新从七张武将牌中获得并立即触发一个技能。")
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local gens = {}
    local all = Fk:getAllGenerals()
    if self.cost_data == "tshujl_chuangshi_choose" then
      room:addPlayerMark(player,"tshujl__chuangshi_choose",1)
      local cgens = table.random(all,6)
      local ask = {}
      for _, g in ipairs(cgens) do
        table.insert(ask,g.name)
      end
      gens = room:askForGeneral(player,ask,3,false)
    elseif self.cost_data ==  "tshujl_chuangshi_random" then
      local cgens = table.random(all,7)
      for _, g in ipairs(cgens) do
        table.insert(gens,g.name)
      end
    end
    local black = {"jbsao__wuyaowang","jbs_sairen","zuoci","os__zhaoxiang","zhaoxiang","ol_ex__zuoci","buqun__ailing","starsp__xiahoudun"}
    local wt = table.simpleClone(gens)
    for _, g in ipairs(wt) do
      if Fk.generals[g].package.extensionName == "tshu" or table.contains(black,g)   then
        table.removeOne(gens,g)
      end
    end
    if #gens <= 0 then  room:doBroadcastNotify("ShowToast","请不要选择禁将。") return end
      local skills = {}
      local choices = {}
      for _, general_name in ipairs(gens) do
        local general = Fk.generals[general_name]
        local g_skills = {}
        for _, skill in ipairs(general.skills) do
            table.insertIfNeed(g_skills, skill.name)
        end
        for _, s_name in ipairs(general.other_skills) do
          local skill = Fk.skills[s_name]
            table.insertIfNeed(g_skills, skill.name)
        end
        table.insertIfNeed(skills, g_skills)
        if #choices == 0 and #g_skills > 0 then
          choices = {g_skills[1]}
        end
      end
      if #choices > 0 then
        local result = player.room:askForCustomDialog(player, self.name,
        "packages/tshu/qml/ChooseGeneralSkillsBox2.qml", {
          gens, skills,1,1, "#tshujl_chuangshi-choice",true
        })
        if result == "" and player.id < 0 then
          local h = table.random(skills)
          choices = table.random(h,1)
        else
          choices = json.decode(result)
        end
        

        if (result ~= "" or player.id < 0 )and #choices > 0 then
          local mark = player:getTableMark(self.name)
          room:setPlayerMark(player,self.name,{})
          if #mark > 0 then
            room:handleAddLoseSkills(player, "-"..table.concat(mark, "|-"), nil,false)
          end
          
        local mark = player:getTableMark(self.name)
        for _, sk in ipairs({choices}) do
          table.forEach(sk, function(s) if not player:hasSkill(s) then table.insertIfNeed(mark,s) end end)
        end
        room:setPlayerMark(player,self.name,mark)
        room:handleAddLoseSkills(player, table.concat(choices, "|"), nil,false)
        local tskills = {}
        local s2 = Fk.skills[choices[1]]
      table.insertIfNeed(tskills,s2)
      for _, s3 in ipairs(s2.related_skills) do
        table.insertIfNeed(tskills,s3)
      end
        for _, s in ipairs(tskills) do
          if s:isInstanceOf(TriggerSkill) and type(s.events) == "table" and (table.contains(s.events,fk.GameStart) or table.contains(s.events, fk.GamePrepared)) then
            local events = {fk.GamePrepared, fk.GameStart}
            for i = #events, 1, -1 do
              if not table.contains(s.events, events[i]) then
                table.remove(events, i)
              end
            end
            for _, e in ipairs(events) do
              if s:triggerable(e, player, player, nil) then
                s:trigger(e, player, player, nil)
              end
            end
          elseif s:isInstanceOf(ActiveSkill) and player:hasSkill(s.name) then
            room:askForUseActiveSkill(player,s.name,"是否发动技能 "..Fk:translate(s.name).." ？",true)
            --[[
          elseif s:isInstanceOf(TriggerSkill) and type(s.events) == "table" and player:hasSkill(s.name) then
            local e = s.events
            local be = {fk.GamePrepared,fk.GameStart,fk.CardResponding,fk.CardUseFinished,fk.CardUsing,fk.CardEffectCancelledOut,fk.CardRespondFinished,fk.CardEffectFinished,fk.CardEffecting,fk.DrawNCards,fk.BeforeDrawCard}
            for _, b in ipairs(be) do
              table.removeOne(e,b)
            end
            if #e > 0 then
              room:useSkill(player,s, function()
                return s:use(table.random(e), player, player)
              end)
            end
          ]]
          end
        end
        end
      end
  end
}
local mieshi = fk.CreateTriggerSkill{
  name = "tshujl__mieshi",
  anim_type = "offensive",
  global = true,
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
  if player.dead then return false end
    if event == fk.TargetSpecified then
      return target == player  and player:getMark("tshujl__sajia") > 0 and math.random(1,10) <= 8 and not data.card.is_damage_card and not player.dead and data.firstTarget and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 
    end
  end,
  on_cost = function (self, event, target, player, data)
    local tars = AimGroup:getAllTargets(data.tos)
    table.removeOne(tars,player.id)
    if #tars <= 0 then return end
    local to = player.room:askForChoosePlayers(player,tars, 1, 1, "灭世：选择其中一个目标令其本回合无法使用、打出手牌或至多两个技能失效。", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local choices = {"技能失效","禁用手牌"}
    local choice = room:askForChoice(player,choices,self.name,"灭世：选择要对 "..Fk:translate(to.general).." 进行的操作。")
    if choice == "技能失效" then
      local skills = {}
      for _, s in ipairs(to.player_skills) do
        if not (s.attached_equip or s.name[#s.name] == "&"  or string.sub(s.name, 1,1) == "#") then
          table.insert(skills,s.name)
        end
      end
      if #skills < 1 then return end
      local gskills = T.choosemarkskillask(player,skills,room,1,2,"选择 "..Fk:translate(to.general).." 的至多两个技能失效")
      if not (gskills and #gskills >= 1) then 
        gskills = table.random(skills,2) 
      end
      local mark = type(to:getMark("@@"..self.name.."-turn")) == "table" and to:getMark("@@"..self.name.."-turn") or {}
      table.insertTableIfNeed(mark,gskills)
      room:setPlayerMark(to,"@@"..self.name.."-turn",mark)
    elseif choice == "禁用手牌" then
      room:setPlayerMark(to,"@@"..self.name.."2-turn",1)
    end
  end,
}
local mieshi_trigger = fk.CreateTriggerSkill{
  name = "#tshujl__mieshi_trigger",
  anim_type = "offensive",
  global = true,
  priority = 0.5,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
  if player.dead then return false end
    return target == player  and player:getMark("tshujl__sajia") > 0 and data.to and data.to ~= player and math.random(1,10) <= 8 and data.to.hp > data.damage and
      player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "灭世：是否令对 "..Fk:translate(data.to.general).." 造成的伤害等同于其体力值？")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(mieshi.name)
    data.damage = data.to.hp
  end,
}
local mieshi_skill = fk.CreateInvaliditySkill {
  name = "#tshujl__mieshi_skill",
  global = true,
  priority = 15,
  invalidity_func = function(self, from, skill)
    local zb = type(from:getMark("@@"..mieshi.name.."-turn")) == "table" and from:getMark("@@"..mieshi.name.."-turn") or {}
      return table.contains(zb,skill.name)
  end
}
local mieshi_prohibit = fk.CreateProhibitSkill{
  name = "#tshujl__mieshi_prohibit",
  global = true,
  prohibit_use = function(self, player, card)
    if player:getMark("@@"..mieshi.name.."2-turn") > 0 then
      local cardlist = Card:getIdList(card)
      return #cardlist > 0 and table.every(cardlist, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@@"..mieshi.name.."2-turn") > 0 then
      local cardlist = Card:getIdList(card)
      return #cardlist > 0 and table.every(cardlist, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
}
local matai = fk.CreateTriggerSkill{
  name = "tshujl__matai",
  mute = true,
  global = true,
  priority = 11,
  frequency = Skill.Compulsory,
  events = {fk.Deathed,fk.DamageInflicted,fk.PreHpLost},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageInflicted or event == fk.PreHpLost then
      return target == player and player:getMark("@@"..self.name.."-turn") > 0
    elseif event == fk.Deathed then
      return target == player and player:getMark("tshujl__sajia") > 0 and player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and player:getMark(self.name) == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageInflicted or event == fk.PreHpLost then
      room:notifySkillInvoked(player,self.name,"drawcard")
      player:broadcastSkillInvoke(self.name)
      return true
    elseif event == fk.Deathed then
      room:notifySkillInvoked(player,self.name,"big")
      player:broadcastSkillInvoke(self.name)
      room:notifySkillInvoked(player,self.name,"support")
      room:revivePlayer(player, false)
      room:setPlayerMark(player,"@@"..self.name.."-turn",1)
      if player.maxHp < 1 then return end
      room:changeMaxHp(player,3)
      room:setPlayerProperty(player, "hp", player.maxHp)
      player:drawCards(5,self.name)
      room:setPlayerMark(player,self.name,1)
    end
  end,
  refresh_events = {fk.GameOverJudge,fk.BuryVictim},
  can_refresh = function (self, event, target, player, data)
    return target == player  and player:getMark("tshujl__sajia") > 0 and player.maxHp > 0 and player:usedSkillTimes(self.name, Player.HistoryGame) == 0 
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    return
  end,
}
mieshi:addRelatedSkill(mieshi_trigger)
mieshi:addRelatedSkill(mieshi_prohibit)
mieshi:addRelatedSkill(mieshi_skill)
chuangshi:addRelatedSkill(chuangshi_active)
sajia:addSkill(chuangshi)
sajia:addSkill(mieshi)
sajia:addSkill(matai)
Fk:loadTranslationTable{
  ["tshujl__sajia"] = "撒迦",

  ["tshujl__chuangshi"] = "创世",
  [":tshujl__chuangshi"] = "锁定技，你的回合开始时或当你受到伤害后，你有90%概率可以重新从7张武将牌(5次自由选将机会)中获得其中一个技能,若为主动技或游戏开始时机可发动的技能则立即触发一次,获得新技能的同时会失去上次以此法获得的技能（每回合限触发两次）。",

  ["#tshujl_chuangshi-choice"] = "创世：选择获得1个技能,或保留上次技能。",
  ["tshujl_chuangshi_random"] = "随机武将",
  ["tshujl_chuangshi_choose"] = "自选武将",
  ["#tshujl_chuangshi_active"] = "创世",
  ["#tshujl__mieshi_trigger"] = "灭世",
  ["#tshujl__mieshi_prohibit"] = "灭世",
  ["#tshujl__mieshi_skill"] = "灭世",
  ["@@tshujl__mieshi-turn"] = "灭世:技能失效",
  ["@@tshujl__mieshi2-turn"] = "灭世:禁用手牌",
  
  ["tshujl__mieshi"] = "灭世",
  [":tshujl__mieshi"] = "锁定技，你使用非伤害类牌指定其他角色为目标时，有80%概率可以令其中一个目标本回合至多两个技能失效或无法使用、打出手牌（此项每回合限触发一次）；当你对其他角色造成伤害时，有80%概率可以令此次伤害量等同受伤目标当前体力值（此项每回合限触发一次）。",
  ["#tshujl__mieshi-invoke"] = "灭世：你可以令你对 %arg 造成的伤害等同于其体力值。",
  ["tshujl__matai"] = "马太",
  [":tshujl__matai"] = "锁定技，你死亡后立即复活，然后免疫所有伤害与体力流失直到本回合结束（每局游戏限触发一次）。",
  ["@@tshujl__matai-turn"] = "马太福音",
  [":tshujl__sajia"] = "<br>〖创世〗:锁定技，你的回合开始时或当你受到伤害后，你有90%概率可以从7张武将牌(5次自由选将机会)中获得其中一个技能,若为主动技或游戏开始时机可发动的技能则立即触发一次，获得新技能的同时会失去上次以此法获得的技能（每回合限触发两次）。"..
  "<br>〖灭世〗:锁定技，你使用非伤害类牌指定其他角色为目标时，有80%概率可以令其中一个目标本回合至多两个技能失效或无法使用、打出手牌（此项每回合限触发一次）；当你对其他角色造成伤害时，有80%概率可以令此次伤害量等同受伤目标当前体力值（此项每回合限触发一次）。"..
  "<br>〖马太〗:锁定技，你死亡后立即复活，然后免疫所有伤害与体力流失直到本回合结束（每局游戏限触发一次）。",

  ["$tshujl__chuangshi1"] = "我创造，我毁灭，我就是上帝。",
  ["$tshujl__chuangshi2"] = "再给我讲讲快乐王子的故事吧，弥赛亚。",
  ["$tshujl__chuangshi3"] = "弥赛亚，不要把我给你的金子给别人。",
  ["$tshujl__mieshi1"] = "以后，你就是上帝的仆人了。",
  ["$tshujl__mieshi2"] = "龙角、尾巴、辫子，这三个地方不能碰。",
  ["$tshujl__matai1"] = "Twinkle twinkle little star~<br>额..咳，你什么都没听到。",
  ["$tshujl__matai2"] = "一个冬天就够了？我可以陪你一个四季。",
}

return extension